package com.testDemo.hutool;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author : zouren
 * @date : 2020/3/26 14:15
 */
public class JSONPathTest {


    @Test
    public void testSetV2() {
        JSONArray root = new JSONArray();
        JSONObject row = new JSONObject();
        row.put("a", "a1");
        root.add(row);
        JSONObject row1 = new JSONObject();
        row1.put("a", "a2");
        root.add(row1);
        Object byPath = JSONUtil.getByPath(root, "$..a");

        System.out.println(byPath);

    }

    @Test
    public void test1() {
        String json = "  {btn: {\n" +
                "    sp0Res: {\n" +
                "      sp0Res001001: '保存',\n" +
                "      sp0Res003001: 'Search',\n" +
                "      sp0Res003002: 'Reset',\n" +
                "      sp0Res003003: 'Edit',\n" +
                "      sp0Res003004: 'View',\n" +
                "      sp0Res003005: 'Collect',\n" +
                "      sp0Res003005001: 'Confirm',\n" +
                "      sp0Res003005003: 'Cancel',\n" +
                "      sp0Res003005002: 'Upload',\n" +
                "      sp0Res003006: '+ New Appointment',\n" +
                "      sp0Res003007: 'Cancel',\n" +
                "      sp0Res003008: 'Create Appointment',\n" +
                "      sp0Res003007001: 'Confirm',\n" +
                "      sp0Res003007002: 'Cancel',\n" +
                "      sp0Res003007003: 'Upload'\n" +
                "    }\n" +
                "  }}";
        JSONObject object = JSONUtil.parseObj(json);
        String byPath = object.getByPath("$.btn.sp0Res.sp0Res001001", String.class);
        System.out.println(byPath);

        String byPath1 = object.getByPath("$..btn..sp0Res001001", String.class);
        System.out.println(byPath1);

        List<String> read = JsonPath.read(object, "$..btn..sp0Res001001");

           System.out.println(read);



    }
}
